package com.parking.common.util;

import java.io.StringWriter;
import java.io.Writer;

import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;

/**
 * JSON工具集
 * @author dyl
 *
 */

public class JsonUtil {
	public static String objectConverterJson(Object obj) {
		try {
			ObjectMapper mapper = new ObjectMapper();  
	        StringWriter jsonData = new StringWriter();  
	        mapper.writeValue(jsonData, obj);
	        return jsonData.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void sendJson(String json, HttpServletResponse response) {
		Writer out = null;
		try {
			response.setContentType("application/json");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			out.write(json);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void sendJsonp(String jsonp, HttpServletResponse response) {
		Writer out = null;
		try {
			response.setContentType("text/javascript");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			out.write(jsonp);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
